package top.heathylife.pagehelperx.converter;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import top.heathylife.pagehelperx.wrappers.FunctionWrapper;
import top.heathylife.pagehelperx.other.Get;
import top.heathylife.pagehelperx.other.TypeFunction;

import java.util.function.Predicate;

/**
 * like 比较器
 *
 * @author maoxiaomeng
 */
@Slf4j
public class LikeConverter<T> implements PredicateConverter<T> {

    @Override
    public Predicate<T> toPredicate(FunctionWrapper<T> functionWrappers) {
        return e -> {
            TypeFunction<T, ?> key = functionWrappers.getKey();
            Object[] value = functionWrappers.getValue();
            String name = Get.name(key);
            log.debug("过滤条件:[like]:key:[{}],value:[{}]", name, value[0]);
            JSON parse = JSONUtil.parse(e);
            Object byPath = parse.getByPath(name);
            return StrUtil.toString(byPath).contains(StrUtil.toString(value[0]));

        };
    }
}

